import 'dart:io';
import 'package:device_info/device_info.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:management/utils/log_utils.dart';

///设备信息
class DeviceHelper {
  static void getDeviceScreenInfo(BuildContext context) {
    var queryData = MediaQuery.of(context);
    MyLog.i("""设备屏幕像素密度：${queryData.devicePixelRatio}
    
    获取逻辑宽度高度
    width: ${queryData.size.width}
    height：${queryData.size.height}
    
    实际屏幕分辨率
    width: ${queryData.size.width * queryData.devicePixelRatio}
    height: ${queryData.size.height * queryData.devicePixelRatio}
    """);
  }

  ///设备信息
  static Future getDeviceInfo() async {
    try {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      if (Platform.isAndroid) {
        AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        var mapInfo = _readAndroidBuildData(androidInfo);
        var sdkInt = mapInfo["version.sdkInt"];
        if (sdkInt == 29) {
          await SpUtil.putInt("sdkInt", sdkInt);
        }
        MyLog.i(mapInfo.toString());
      } else if (Platform.isIOS) {
        IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
        MyLog.i(_readIosDeviceInfo(iosInfo).toString());
      }
    } catch (e) {
      MyLog.e(e.toString());
    }
  }

  //android 设备
  static Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
    return <String, dynamic>{
      'version.securityPatch': build.version.securityPatch,
      'version.sdkInt': build.version.sdkInt,
      'version.release': build.version.release,
      'version.previewSdkInt': build.version.previewSdkInt,
      'version.incremental': build.version.incremental,
      'version.codename': build.version.codename,
      'version.baseOS': build.version.baseOS,
      'board': build.board,
      'bootloader': build.bootloader,
      'brand': build.brand,
      'device': build.device,
      'display': build.display,
      'fingerprint': build.fingerprint,
      'hardware': build.hardware,
      'host': build.host,
      'id': build.id,
      'manufacturer': build.manufacturer,
      'model': build.model,
      'product': build.product,
      'supported32BitAbis': build.supported32BitAbis,
      'supported64BitAbis': build.supported64BitAbis,
      'supportedAbis': build.supportedAbis,
      'tags': build.tags,
      'type': build.type,
      'isPhysicalDevice': build.isPhysicalDevice,
      'androidId': build.androidId
    };
  }

  //ios 设备
  static Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
    return <String, dynamic>{
      'name': data.name,
      'systemName': data.systemName,
      'systemVersion': data.systemVersion,
      'model': data.model,
      'localizedModel': data.localizedModel,
      'identifierForVendor': data.identifierForVendor,
      'isPhysicalDevice': data.isPhysicalDevice,
      'utsname.sysname:': data.utsname.sysname,
      'utsname.nodename:': data.utsname.nodename,
      'utsname.release:': data.utsname.release,
      'utsname.version:': data.utsname.version,
      'utsname.machine:': data.utsname.machine,
    };
  }
}
